package com.service.calculate.vehicle;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.service.bean.device.Vehicle;
import com.service.bean.garbage.GarbageBean;
import com.service.bean.garbage.GarbageData;
import com.service.other.Logger;
import com.service.socket.protocol.Conversion;
import com.service.socket.protocol.GarbageDataProtocol;
import com.service.socket.protocol.Packet;

/**
 * 解析垃圾称重数据
 * @author zyf
 *
 */
public class GarbageCal {

	private GarbageBean bean1;
	private GarbageBean bean2;
	private Vehicle vehicle;
	private Packet packet;
	private GarbageData data;
	
	public GarbageCal(Packet packet, Vehicle vehicle) {
		this.packet = packet;
		this.vehicle = vehicle;
		data = GarbageDataProtocol.Parser(packet.dataArea);
	}
	
	public List<GarbageBean> cal() {
		List<GarbageBean> beans = new ArrayList<GarbageBean>();
		bean1 = new GarbageBean();
		bean2 = new GarbageBean();
		
		try {
			bean1.setRawData(packet.getRawData());
			bean2.setRawData(packet.getRawData());
			bean1.setGpsNo(Long.parseLong(packet.getDeviceNo()));
			bean2.setGpsNo(Long.parseLong(packet.getDeviceNo()));
			bean1.setEquipmentId(vehicle.equipmentId);
			bean2.setEquipmentId(vehicle.equipmentId);
			
			bean1.setDriverNumber(data.driverNo == null || "".equals(data.driverNo.trim()) ? "" : Conversion.hexStringToGb2312(data.driverNo));
			bean2.setDriverNumber(data.driverNo == null || "".equals(data.driverNo.trim()) ? "" : Conversion.hexStringToGb2312(data.driverNo));
			bean1.setRfid(data.firstTrashId == null || "".equals(data.firstTrashId.trim()) ? "" : Conversion.hexStringToGb2312(data.firstTrashId));
			bean2.setRfid(data.secondTrashId == null  || "".equals(data.secondTrashId.trim()) ? "" : Conversion.hexStringToGb2312(data.secondTrashId));
			
			double collectionAmt1 = new BigDecimal(
					Integer.parseInt(data.firstTrashWeight, 16) / 100.00).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			bean1.setCollectionAmt(collectionAmt1);
			double collectionAmt2 = new BigDecimal(
					Integer.parseInt(data.secondTrashWeight, 16) / 100.00).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			bean2.setCollectionAmt(collectionAmt2);
			
			bean1.setGpsTime(data.getGpsTime());
			bean2.setGpsTime(data.getGpsTime());
			
			double lat = new BigDecimal(
					Integer.parseInt(data.latitude, 16) / 1800000.00).setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue();
			double lng = new BigDecimal(
					Integer.parseInt(data.longtitude, 16) / 1800000.00).setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue();
			
			bean1.setBaiDuLat(lat);
			bean1.setBaiDuLng(lng);
			bean2.setBaiDuLat(lat);
			bean2.setBaiDuLng(lng);
			
			beans.add(bean1);
			beans.add(bean2);
			
		} catch (Exception e) {
			Logger.err(e);
		}
		return beans;
	}
	
}
